<?php

namespace App\Models;

use App\Models\FhTag;
use Illuminate\Database\Eloquent\Model;
class FhQuestion extends Model
{
    protected $table = 'fh_question';

    public static function boot()
    {
        parent::boot();

        static::saving(function ($model) {
           
            $tags = $model->tag;
            $data = FhTag::whereIn('id',explode(',',$tags))->select("c_name","url")->get();

            $content = $model->content;
            foreach($data as $v){
                $htmls = '<a style="color:#7bcd40;" href="'.$v['url'].'" title="'.$v['c_name'].'" target="_blank">'.$v['c_name'].'</a>';
                $content = str_replace($v['c_name'],$htmls,$content);
            }
            $model->content = $content;
            if(strpos($model->url, "/blog/") === false){
                $model->url = "/blog/".$model->url;
            }
        });
    }
    
}
